package controller

import (
	"blog-back-end/common"
	"blog-back-end/models/user"
	"blog-back-end/service"
	"github.com/gin-gonic/gin"
)

func Login(context *gin.Context) {
	username := context.Query("username")
	password := context.Query("password")
	token, err := service.Login(username, password)
	res := common.NewResult(context)
	if err == nil {
		res.Success(token)
	} else {
		res.Error(500, err.Error())
	}
	return

}

func Register(context *gin.Context) {
	var userRegisterData user.RegisterDTO
	err := context.ShouldBind(&userRegisterData)
	res := common.NewResult(context)
	if err == nil {
		err = service.Register(&userRegisterData)
	}
	if err == nil {
		//TODO data==nil的情况
		res.Success(nil, "注册成功")
	} else {
		res.Error(500, err.Error())
	}
	return

}
